/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package SaveMessage;

import DataAccess.FindResult;
import DataAccess.SavemessageDA;
import entity.House;
import entity.Savemessage;
import entity.SwhUser;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

/**
 *
 * @author Mr-Quick
 */
public class SavemessageService {

    EntityManager em;

    public SavemessageService() {
        String persistenceUnitName = "SweetHomePU";
        EntityManagerFactory factory = Persistence.createEntityManagerFactory(persistenceUnitName);
        em = factory.createEntityManager();
    }

    public String Createsavemessage(SwhUser su, House house) {
        try {
            if (FindIduserandIdhouseinSavemessage(su, house) == null) {
                Savemessage sm = new Savemessage();
                sm.setIdmessage(0);
                sm.setIDUser(su);
                sm.setIdHouse(house);
                SavemessageDA sda = new SavemessageDA(em);
                em.getTransaction().begin();
                sda.create(sm);
                em.getTransaction().commit();
                return "true";
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public Savemessage FindIduserandIdhouseinSavemessage(SwhUser user, House house) {
        SavemessageDA sda = new SavemessageDA(em);
        //FindResult<Savemessage> fs = sda.findAbsolutely("IDUser", "IdHouse", user, house);
        Query q = em.createQuery("select s from  Savemessage s where ((s.iDUser = :iduser) and (s.idHouse = :idhouse))", Savemessage.class);
        q.setParameter("iduser", user);
        q.setParameter("idhouse", house);
        List<Savemessage> frs = q.getResultList();
        if (frs.isEmpty()) {
            return null;
        } else {
            return frs.get(0);
        }
    }

    public List<Savemessage> Findallmessagebyidhouse(House house) {
        SavemessageDA sda = new SavemessageDA(em);
        List<Savemessage> list = sda.findAbsolutely("idHouse", house);
        if (list.isEmpty()) {
            return null;
        } else {
            return list;
        }
    }

    public List<Savemessage> Findallmessagebyiduser(SwhUser user) {
        SavemessageDA sda = new SavemessageDA(em);
        List<Savemessage> list = sda.findAbsolutely("iDUser", user);
        if (list.isEmpty()) {
            return null;
        } else {
            return list;
        }
    }

    public void deletesavemessagebyidhouse(List<Savemessage> list) {
        SavemessageDA sda = new SavemessageDA(em);
        em.getTransaction().begin();
        for (int i = 0; i < list.size(); i++) {
            sda.remove(list.get(i));
        }
        em.getTransaction().commit();
    }

    public String deleteonesavemessage(Savemessage save) {
        try {
            
            em.getTransaction().begin();
            em.remove(save);
            em.getTransaction().commit();
            return "true";
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }
    
        public Savemessage Findsavemessagebyidsavemessage(int idsavemessage) {
        SavemessageDA sda = new SavemessageDA(em);
        List<Savemessage> list = sda.findAbsolutely("idmessage", idsavemessage);
        if (list.isEmpty()) {
            return null;
        } else {
            return list.get(0);
        }
    }
}
